<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
    <link rel="stylesheet" href="/css/main.css">
    <link rel="stylesheet" href="/css/demo.css">
    <style>
        body {
            background-image: url("/images/background.jpg");
            background-repeat: repeat-x;
            background-size: cover;
            font-family: Arial, sans-serif;
            background-color: #ffffff;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .login-box {
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        h2 {
            text-align: center;
        }

        .user-box input[type="text"],
        .user-box input[type="password"] {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            background-color: transparent;
            border: 1px solid #ccc;
            border-radius: 3px;
        }

        .user-box label {
            display: block;
            margin-bottom: 5px;
        }

        a {
            display: inline-block;
            width: 100%;
            padding: 10px;
            background-color: transparent;
            color: #fff;
            border: none;
            border-radius: 3px;
            cursor: pointer;
            text-align: center;
            text-decoration: none;
            position: relative;
        }

        a span {
            position: absolute;
            display: block;
        }

        a:hover {
            background-color: #0056b3;
        }

        .register-link {
            margin-top: 10px;
            text-align: center;
        }

        .register-link a {
            color: #007BFF;
            text-decoration: none;
        }

        .register-link a:hover {
            text-decoration: underline;
        }

        #errorMessage {
            color: red;
            text-align: center;
            margin-top: 10px;
        }
    </style>
</head>

<body class="dark">
<div class="container">
    <div class="login-box">
        <h2>登录</h2>
        <form id="loginForm">
            <div class="user-box">
                <input type="text" id="username" required>
                <label>用户名</label>
            </div>
            <div class="user-box">
                <input type="password" id="password" required>
                <label>密码</label>
            </div>
            <a href="#" onclick="login(event)">
                <span></span>
                <span></span>
                <span></span>
                <span></span>
                登录
            </a>
        </form>
        <p id="errorMessage"></p>
        <div class="register-link">
            还没有账号？<a href="/register">注册</a>
        </div>
    </div>
</div>
<script>
    document.addEventListener('DOMContentLoaded', function () {
        const registeredUsername = localStorage.getItem('registeredUsername');
        const registeredPassword = localStorage.getItem('registeredPassword');

        if (registeredUsername && registeredPassword) {
            document.getElementById('username').value = registeredUsername;
            document.getElementById('password').value = registeredPassword;
        }
    });

    function login(event) {
        event.preventDefault();
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;

        fetch('/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: `username=${username}&password=${password}`
        })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    window.location.href = '/home';
                } else {
                    document.getElementById('errorMessage').innerHTML = data.message;
                }
            })
            .catch(error => {
                console.error('请求出错:', error);
                document.getElementById('errorMessage').innerHTML = '网络错误，请稍后重试';
            });
    }
</script>
</body>

</html>